<?php
namespace Home\Controller;
use Think\Controller;
class JdController extends ZMController {
	private static $apiKey='eNJkMqWzSYj3d9Df';
	private static $region=array('建德市');
	public function _initialize(){
		$pdate=I('pdate');//秘钥时间
		$sapiKey=I('apiKey');//对方数字签名
		$signature=base64_encode(md5($pdate.self::$apiKey));
		if($sapiKey!=$signature){//对比失败
			$this->errorreturn("apiKey错误");
		}
		$this->region=9;
	}
	public function hospital(){//医院列表
		$region=$this->region;//建德
		$where=array();
		$where['org_id']=array('NEQ','');
		if(!empty($region)){
			$where['region']=$region;
		}
		$Model=M()->db(1,'DB_CONFIG1');
		$res=$Model->table('ddwk_hospital')->where($where)->field("hid as id,name")->select();
		if(empty($res)){
			$res=array();
		}
		$this->ajax($res);
	}
	public function yearTypeCollect(){
		$region=$this->region;
		$collect=R('Message/yearTypeCollect',array($region));
		$arr=array();
		$i=0;
		foreach($collect as $row){
			switch($i){
				case 0:
					$arr['totalWeight']=$row;
					break;
				case 1:
					$arr['infectWeight']=$row;
					break;
				case 2:
					$arr['injureWeight']=$row;
					break;
				case 3:
					$arr['chemistryWeight']=$row;
					break;
				case 4:
					$arr['medicineWeight']=$row;
					break;
				case 5:
					$arr['pathWeight']=$row;
					break;
				case 6:
					$arr['othersWeight']+=$row;
					break;
				case 7:
					$arr['othersWeight']+=$row;
					break;
			}
			$i++;
		}
		$this->ajax($arr);
	}
	public function hospCollect(){//医院收集详细情况
		try{
			$pdate=I('date');//时间
			$hid=I('hid');
			$region=$this->region;//建德
			if(empty($pdate)){
				E('未定义date');
			}
			if(empty($hid)){//医院为空定义为全区所有医院
				$Model=M()->db(1,'DB_CONFIG1');
				$arr=$Model->query("select hid from ddwk_hospital where region in ($region) and hid<>11");
				foreach($arr as $re){
					$hid.=','.$re['hid'];
				}
				$hid=ltrim($hid,',');
			}
			$res=R("Collect/hcustcollect",array($hid,$pdate));
			$arr=array();
			foreach($res as $v){
				unset($data);
				$data['deptName']=$v['custname'];
				$data['recycleTime']=strtotime($v['ctime']);
				$data['recycleUserName']=$v['cname'];
				$data['nurseName']=$v['sname'];
				$data['infectWeight']=$v['weight_0'];
				$data['injureWeight']=$v['weight_1'];
				$data['pathWeight']=$v['weight_5'];
				$data['medicineWeight']=$v['weight_4'];
				$data['othersWeight']=$v['weight_2']+$v['weight_7'];
				$arr[]=$data;
			}
			$this->ajax($arr);
		}
		catch(\Exception $e){
		    $message=$e->getMessage();
			$this->errorreturn($message);
		}
	}
	public function hospUnload(){//医院入库详细情况
		try{
			$pdate=I('date');//时间
			$hid=I('hid');
			$region=$this->region;//建德
			if(empty($pdate)){
				E('未定义date');
			}
			if(empty($hid)){//医院为空定义为全区所有医院
				$Model=M()->db(1,'DB_CONFIG1');
				$arr=$Model->query("select hid from ddwk_hospital where region in ($region) and hid<>11");
				foreach($arr as $re){
					$hid.=','.$re['hid'];
				}
				$hid=ltrim($hid,',');
			}
			$res=R("Collect/UnloadSum",array($hid,$pdate));
			$arr=array();
			foreach($res as $v){
				unset($data);
				$data['recycleDate']=strtotime($v['ctime']);
				$data['instoreUserName']=$v['sname'];
				$data['infectWeight']=$v['weight_0'];
				$data['injureWeight']=$v['weight_1'];
				$data['pathWeight']=$v['weight_5'];
				$data['medicineWeight']=$v['weight_4'];
				$data['othersWeight']=$v['weight_2']+$v['weight_7'];
				$arr[]=$data;
			}
			$this->ajax($arr);
		}
		catch(\Exception $e){
		    $message=$e->getMessage();
			$this->errorreturn($message);
		}
	}
	public function hospTotalCollect(){//各机构每日产生量汇总
		try{
			$pdate=I('date');//时间
			$hid=I('hid');
			$region=$this->region;//建德
			if(empty($pdate)){
				E('未定义date');
			}
			if(empty($hid)){
				E('未定义hid');
			}
			$type=array(0,1,2,3,4,5,6);
			$Model=M()->db(1,'DB_CONFIG1');
			$res=$Model->query("select cid from ddwk_collect where pdate='$pdate' and hid in ($hid)");
			if(!empty($res)){//不为空
				$cid='';
				foreach($res as $v){
					$cid.=','.$v['cid'];
				}
				$cid=ltrim($cid,',');
			}
			else{
				E("无数据");
			}
			$arr=array();
			foreach($type as $v){
				switch($v){
					case 0:
						$arr['infectWeight']=R("Message/dayCollectHosp",array($cid,$v));//感染性
						break;
					case 1:
						$arr['injureWeight']=R("Message/dayCollectHosp",array($cid,$v));//损伤性
						break;
					case 2:
						$arr['chemistryWeight']=R("Message/dayCollectHosp",array($cid,$v));//化学性
						break;
					case 3:
						$arr['medicineWeight']=R("Message/dayCollectHosp",array($cid,$v));//药物性
						break;
					case 4:
						$arr['pathWeight']=R("Message/dayCollectHosp",array($cid,$v));//病理性
						break;
					case 5:
						$arr['othersWeight']+=R("Message/dayCollectHosp",array($cid,$v));//其他性
						break;
					case 6:
						$arr['othersWeight']+=R("Message/dayCollectHosp",array($cid,$v));//其他性
						break;
				}
			}
			$arr['totalWeight']=$arr['infectWeight']+$arr['injureWeight']+$arr['chemistryWeight']+$arr['pathWeight']+$arr['othersWeight'];
			$this->ajax($arr);
		}
		catch(\Exception $e){
		    $message=$e->getMessage();
			$this->errorreturn($message);
		}
	}
	public function totalCollect(){//区间内收集总量
		try{
			$startdate=I('startdate');
			$enddate=I('enddate');
			if($startdate>$enddate){
				E('时间错误');
			}
			$Model=M()->db(1,'DB_CONFIG1');
			$region=$this->region;
			$arr=$Model->query("select hid from ddwk_hospital where region in ($region) and hid<>11");
			foreach($arr as $re){
				$hid.=','.$re['hid'];
			}
			$hid=ltrim($hid,',');//医院
			$pdate=$startdate;
			$arr=array();
			$a=array();
			/*while($pdate<=$enddate){
				$a=$this->total($pdate,$hid);
				$arr['infectWeight']+=$a['infectWeight'];
				$arr['injureWeight']+=$a['injureWeight'];
				$arr['chemistryWeight']+=$a['chemistryWeight'];
				$arr['medicineWeight']+=$a['medicineWeight'];
				$arr['pathWeight']+=$a['pathWeight'];
				$arr['othersWeight']+=$a['othersWeight'];
				$arr['totalWeight']+=$a['totalWeight'];
				$pdate=date('Y-m-d',strtotime($pdate)+86400);
			}*/
			$arr=$this->total($startdate,$enddate,$hid);
			$this->ajax($arr);
		}
		catch(\Exception $e){
		    $message=$e->getMessage();
			$this->errorreturn($message);
		}
	}
	public function total($startdate,$enddate,$hid){
		try{
			$region=$this->region;//建德
			$type=array(0,1,2,3,4,5,6);
			$Model=M()->db(1,'DB_CONFIG1');
			$res=$Model->query("select cid from ddwk_collect where pdate BETWEEN '$startdate' and '$enddate' and hid in ($hid)");
			if(!empty($res)){//不为空
				$cid='';
				foreach($res as $v){
					$cid.=','.$v['cid'];
				}
				$cid=ltrim($cid,',');
			}
			else{
				E("无数据");
			}
			$arr=array();
			foreach($type as $v){
				switch($v){
					case 0:
						$arr['infectWeight']=R("Message/dayCollectHosp",array($cid,$v));//感染性
						break;
					case 1:
						$arr['injureWeight']=R("Message/dayCollectHosp",array($cid,$v));//损伤性
						break;
					case 2:
						$arr['chemistryWeight']=R("Message/dayCollectHosp",array($cid,$v));//化学性
						break;
					case 3:
						$arr['medicineWeight']=R("Message/dayCollectHosp",array($cid,$v));//药物性
						break;
					case 4:
						$arr['pathWeight']=R("Message/dayCollectHosp",array($cid,$v));//病理性
						break;
					case 5:
						$arr['othersWeight']+=R("Message/dayCollectHosp",array($cid,$v));//其他性
						break;
					case 6:
						$arr['othersWeight']+=R("Message/dayCollectHosp",array($cid,$v));//其他性
						break;
				}
			}
			$arr['totalWeight']=$arr['infectWeight']+$arr['injureWeight']+$arr['chemistryWeight']+$arr['pathWeight']+$arr['othersWeight'];
			return $arr;
		}
		catch(\Exception $e){
		    $message=$e->getMessage();
			$this->errorreturn($message);
		}
	}
}